package com.ljl.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljl.ssm.entity.Category;
import com.ljl.ssm.entity.CompleteIn;
import com.ljl.ssm.entity.User;
import com.ljl.ssm.mapper.CategoryMapper;
import com.ljl.ssm.mapper.CompleteInMapper;
import com.ljl.ssm.mapper.RegistMapper;
import com.ljl.ssm.service.ICategoryService;
import com.ljl.ssm.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;

@Service
public class RegisterServiceImpl extends ServiceImpl<RegistMapper, User> implements IRegisterService {

    @Autowired
    RegistMapper registMapper;
    @Autowired
    CompleteInMapper completeInMapper;
    public int select(User user){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",user.getName());
        User result=registMapper.selectOne(queryWrapper);

        if(result!=null)
            return 0;
        else{
            return add(user);
        }
    }
    public int add(User user){
        //插入数据库
        int result=registMapper.insert(user);
//      获取到该user自动生成的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",user.getName());
        //工具人user1，取到想要的id信息就丢
        User user1=registMapper.selectOne(queryWrapper);
        int userId=user1.getId();
       //丢掉工具人，用该userId去插入进complete_in表里的user_id
        CompleteIn completeIn=new CompleteIn();
        completeIn.setUserId(userId);
        completeInMapper.insert(completeIn);
        return result;
    }

    @Override
    public User editPassword(HttpServletRequest request, String password, Model model) {
        User user=new User();
        user=(User)request.getSession().getAttribute("user");
        user.setPassword(password);
        registMapper.updateById(user);
        //工具人user1,目的是为了更新对象字段值
        User user1=new User();
        user1=registMapper.selectById(user.getId());
        //前台显示
        model.addAttribute("password",user1.getPassword());
        return user;
    }
}
